package com.szty.libarytest.other

import android.view.ViewGroup
import androidx.fragment.app.FragmentPagerAdapter

/**
 * 给Viewpager设置适配器，并设置离屏加载数为list.size
 */
fun androidx.viewpager.widget.ViewPager.setBaseAdapter(fm: androidx.fragment.app.FragmentManager, list:List<androidx.fragment.app.Fragment>,behavior:Int = FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT){
    adapter = getBaseFragmentStatePagerAdapter(
        fm,
        list,
        behavior
    )
    offscreenPageLimit = list.size
}

/**
 * 默认的获取FragmentStatePagerAdapter
 */
fun getBaseFragmentStatePagerAdapter(fm: androidx.fragment.app.FragmentManager, list:List<androidx.fragment.app.Fragment>,behavior:Int = FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT): androidx.fragment.app.FragmentPagerAdapter {
    return object: androidx.fragment.app.FragmentPagerAdapter(fm,behavior) {
        override fun getItem(position: Int): androidx.fragment.app.Fragment {
            return list[position]
        }
        override fun getCount(): Int {
            return list.size
        }
        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        }
    }
}